package com.baofu.paycore.biz.paycore.converter;

import com.baofu.paycore.biz.engine.result.CommandResult;
import com.baofu.paycore.biz.external.clear.model.EventRuleResBO;
import com.baofu.paycore.biz.model.BackPayBO;
import com.baofu.paycore.biz.model.PayResBO;
import com.baofu.paycore.biz.model.WebPayBO;
import com.baofu.paycore.common.enums.FundTypeEnum;
import com.baofu.paycore.manager.model.*;
import com.baofu.paycore.manager.model.preAuth.PreAuthBO;
import com.baofu.paycore.manager.model.withdraw.WithdrawBO;
import com.baofu.paycore.manager.model.withdraw.WithdrawFundBO;
import com.baofu.paycore.service.facade.enums.PayStatusEnums;
import com.baofu.paycore.service.facade.enums.PayToolTypeEnum;
import com.baofu.paycore.service.facade.model.PayMessageDTO;

import java.util.Date;

/**
 * BO转换成DTO对象
 * <p>
 *     1、充值接口返回结果信息处理
 * </p>
 * User: kwl  Date: 16/5/6 ProjectName: paycore Version: 5.0.0
 */
public class BO2BOConverter {

    /**
     * 充值请求对象转页面跳转URL对象 rechargeBO to WebPayBO
     *
     * @param rechargeBO      充值请求参数
     * @return    webPayBO 充值返回对象
     */
    public static WebPayBO rechargeBO2WebPayBO(RechargeBO rechargeBO){

        WebPayBO webPayBO = new WebPayBO();
        if(null == rechargeBO) {
            return webPayBO;
        }
        webPayBO.setRealFlag(rechargeBO.isRealFlag());
        webPayBO.setOutPayUrl(rechargeBO.getOutPayUrl());
        webPayBO.setParams(rechargeBO.getParams());
        webPayBO.setPayNo(rechargeBO.getPayNo());
        webPayBO.setStatus(rechargeBO.getStatus());
        webPayBO.setErrorCode(rechargeBO.getErrorCode());
        webPayBO.setErrorMsg(rechargeBO.getErrorMsg());
        return webPayBO;
    }

    /**
     * 充值请求对象你给BO转充值返回对象 rechargeBO to BackPayBO
     *
     * @param rechargeBO      充值请求参数
     * @return    backPayBO 返回对象
     */
    public static BackPayBO rechargeBO2BackPayBO(RechargeBO rechargeBO){

        BackPayBO backPayBO = new BackPayBO();
        if(null == rechargeBO) {
            return backPayBO;
        }

        backPayBO.setPayNo(rechargeBO.getPayNo());
        backPayBO.setRequestBizNo(rechargeBO.getRequestBizNo());
        backPayBO.setRequestOrderNo(rechargeBO.getRequestOrderNo());
        backPayBO.setRequestDate(new Date());
        backPayBO.setRealFlag(rechargeBO.isRealFlag());
        backPayBO.setCcy(rechargeBO.getCcy());
        backPayBO.setAmt(rechargeBO.getAmt());
        backPayBO.setPayDate(new Date());
        backPayBO.setStatus(rechargeBO.getStatus());
        backPayBO.setErrorCode(rechargeBO.getErrorCode());
        backPayBO.setErrorMsg(rechargeBO.getErrorMsg());
        backPayBO.setBankCode(rechargeBO.getBankCode());
        backPayBO.setBankName(rechargeBO.getBankName());
        return backPayBO;
    }

    /**
     * 充值请求对象转页面跳转URL对象 rechargeBO to WebPayBO
     *
     * @param payBO    订单对象
     * @return    webPayBO
     */
    public static WebPayBO payBO2WebPayBO(PayBO payBO){

        WebPayBO webPayBO = new WebPayBO();
        if (null == payBO) {
            return webPayBO;
        }
        webPayBO.setRealFlag(payBO.isRealFlag());
        webPayBO.setOutPayUrl(payBO.getOutPayUrl());
        webPayBO.setPaymentInfo(payBO.getPaymentInfo());
        webPayBO.setParams(payBO.getParams());
        webPayBO.setPayNo(payBO.getPayNo());
        webPayBO.setStatus(payBO.getStatus());
        webPayBO.setErrorCode(payBO.getErrorCode());
        webPayBO.setErrorMsg(payBO.getErrorMsg());
        return webPayBO;
    }

    /**
     * 充值请求对象转页面跳转URL对象 rechargeBO to BackPayBO
     *
     * @param payBO    订单对象
     * @return    backPayBO 返回对象
     */
    public static BackPayBO payBO2BackPayBO(PayBO payBO){

        BackPayBO backPayBO = new BackPayBO();
        if(null == payBO) {
            return backPayBO;
        }

        backPayBO.setPayNo(payBO.getPayNo());
        backPayBO.setRequestBizNo(payBO.getRequestBizNo());
        backPayBO.setRequestOrderNo(payBO.getRequestOrderNo());
        backPayBO.setRequestDate(payBO.getRequestDate());
        backPayBO.setRealFlag(payBO.isRealFlag());
        backPayBO.setCcy(payBO.getCcy());
        backPayBO.setAmt(payBO.getAmt());
        backPayBO.setPayDate(new Date());
        backPayBO.setStatus(payBO.getStatus());
        backPayBO.setPaymentInfo(payBO.getPaymentInfo());
        backPayBO.setErrorCode(payBO.getErrorCode());
        backPayBO.setErrorMsg(payBO.getErrorMsg());
        backPayBO.setResponseDate(payBO.getRespDate());
        return backPayBO;
    }

    /**
     *  预授权响应BO
     *
     * @param preAuthBO     预授权BO
     * @return              BackPayBO
     */
    public static BackPayBO preAuthBO2BackPayBO(PreAuthBO preAuthBO){

        BackPayBO backPayBO = new BackPayBO();
        if(null == preAuthBO) {
            return backPayBO;
        }

        backPayBO.setPayNo(preAuthBO.getPayNo());
        backPayBO.setRequestBizNo(preAuthBO.getRequestBizNo());
        backPayBO.setRequestOrderNo(preAuthBO.getRequestOrderNo());
        backPayBO.setRequestDate(preAuthBO.getRequestDate());
        backPayBO.setRealFlag(preAuthBO.isRealFlag());
        backPayBO.setCcy(preAuthBO.getCcy());
        backPayBO.setAmt(preAuthBO.getAmt());
        backPayBO.setPayDate(new Date());
        backPayBO.setStatus(preAuthBO.getStatus());
        backPayBO.setErrorCode(preAuthBO.getErrorCode());
        backPayBO.setErrorMsg(preAuthBO.getErrorMsg());
        backPayBO.setResponseDate(new Date());
        backPayBO.setFreezeCode(preAuthBO.getSubPreAuthBO().get(0).getFreezeCode());
        return backPayBO;
    }

    /**
     * 转账请求对象转页面跳转URL对象 transfer to BackPayBO
     *
     * @param transferBO    订单对象
     * @return    backPayBO 返回对象
     */
    public static BackPayBO transferBO2BackPayBO(TransferBO transferBO){

        BackPayBO backPayBO = new BackPayBO();
        backPayBO.setPayNo(transferBO.getPayNo());
        backPayBO.setRequestBizNo(transferBO.getRequestBizNo());
        backPayBO.setRequestOrderNo(transferBO.getRequestOrderNo());
        backPayBO.setRequestDate(transferBO.getRequestDate());
        backPayBO.setRealFlag(true);
        backPayBO.setCcy(transferBO.getCcy());
        backPayBO.setAmt(transferBO.getAmt());
        backPayBO.setPayDate(new Date());
        backPayBO.setStatus(transferBO.getStatus());
        backPayBO.setErrorCode(transferBO.getErrorCode());
        backPayBO.setErrorMsg(transferBO.getErrorMsg());
        return backPayBO;
    }

    /**
     * 提现BO对象转换BackPayBO对象
     *
     * @param withdrawBO     提现BO对象
     * @return               backPayBo
     */
    public static BackPayBO withdrawBO2BackPayBO(WithdrawBO withdrawBO){

        BackPayBO backPayBO = new BackPayBO();
        if(null == withdrawBO) {
            return backPayBO;
        }

        backPayBO.setPayNo(withdrawBO.getPayNo());
        backPayBO.setRequestBizNo(withdrawBO.getRequestBizNo());
        backPayBO.setRequestOrderNo(withdrawBO.getRequestOrderNo());
        backPayBO.setRequestDate(withdrawBO.getRequestDate());
        backPayBO.setRealFlag(withdrawBO.isRealFlag());
        backPayBO.setCcy(withdrawBO.getCcy());
        backPayBO.setAmt(withdrawBO.getAmt());
        backPayBO.setResponseDate(new Date());
        backPayBO.setBankName(withdrawBO.getBankName());
        backPayBO.setBankCode(withdrawBO.getBankCode());
        backPayBO.setStatus(withdrawBO.getStatus());
        backPayBO.setErrorMsg(withdrawBO.getErrorMsg());
        backPayBO.setErrorCode(withdrawBO.getErrorCode());

        return backPayBO;
    }

    /**
     * 退款返回结果对象转换
     *
     * @param refundBO    退款BO对象
     * @return            返回结果对象
     */
    public static PayResBO payBO2PayResBO(RefundBO refundBO){

        PayResBO payResBO = new PayResBO();
        if(null == refundBO){
            return payResBO;
        }

        payResBO.setPayNo(refundBO.getPayNo());
        payResBO.setRequestOrderNo(refundBO.getRequestOrderNo());
        payResBO.setRequestBizNo(refundBO.getRequestBizNo());
        payResBO.setRequestDate(refundBO.getRequestDate());

        payResBO.setCcy(refundBO.getCcy());
        payResBO.setAmt(refundBO.getAmt());

        payResBO.setRealFlag(refundBO.isRealFlag());
        payResBO.setStatus(refundBO.getStatus());

        payResBO.setRespDate(refundBO.getRespDate());
        payResBO.setErrorCode(refundBO.getErrorCode());
        payResBO.setErrorMsg(refundBO.getErrorMsg());
        return payResBO;
    }

    /**
     * 封装指令结果
     *
     * @param result        指令结果
     * @param accountRespBO 账务响应
     */
    public static void buildCommandResult(CommandResult result, AccountRespBO accountRespBO){
        result.setRealRtnFlag(accountRespBO.isRealRtn());
        result.setStatus(accountRespBO.getStatus());
        result.setResultCode(accountRespBO.getErrorCode());
        result.setResultMsg(accountRespBO.getErrorMsg());
        result.setResultVO(accountRespBO);
        //记录账务系统返回的会计日和凭证号，用于方向交易时使用
        result.setResponseDate(accountRespBO.getAccDate());
        result.setResponseNo(accountRespBO.getReceiptNo());
    }

    /**
     * 解冻提现单赋值
     *
     * @param withdrawFundBO    解冻提现单
     * @param respBO            响应
     */
    public static void unFreezeWithdrawResult(WithdrawFundBO withdrawFundBO, UnFreezeWithdrawRespBO respBO){
        withdrawFundBO.setStatus(respBO.getStatus());
        withdrawFundBO.setTransDate(respBO.getAccDate());
        withdrawFundBO.setTransReceiptNo(respBO.getWithdrawReceipt());
        withdrawFundBO.setErrorCode(respBO.getErrorCode());
        withdrawFundBO.setErrorReason(respBO.getErrorMsg());
    }

    /**
     * 解冻转账单赋值
     *
     * @param withdrawFundBO    解冻转账单
     * @param respBO            响应
     */
    public static void unFreezeTransferResult(WithdrawFundBO withdrawFundBO, UnFreezeTransferRespBO respBO){

        withdrawFundBO.setStatus(respBO.getStatus());
        withdrawFundBO.setTransDate(respBO.getAccDate());
        withdrawFundBO.setTransReceiptNo(respBO.getTransferReceipt());
        withdrawFundBO.setErrorCode(respBO.getErrorCode());
        withdrawFundBO.setErrorReason(respBO.getErrorMsg());
    }

    /**
     * 会计事件规则返回参数转换成充值请求参数
     *
     * @param eventRuleResBO    会计事件规则返回参数
     * @param rechargeBO        充值请求对象
     */
    public static void eventResBO2RechargeBO(EventRuleResBO eventRuleResBO, RechargeBO rechargeBO,
                                             RechargeFundBO rechargeFundBO){

        rechargeFundBO.setEventNo(eventRuleResBO.getEventNo());
        rechargeBO.setErrorCode(eventRuleResBO.getErrorCode());
        rechargeBO.setErrorMsg(eventRuleResBO.getErrorMsg());
        rechargeBO.setRealFlag(Boolean.TRUE);
        rechargeBO.setStatus(eventRuleResBO.getStatus());
        rechargeFundBO.setStatus(eventRuleResBO.getStatus());
    }

    /**
     * 获取支付消息信息
     *
     * @param payBaseBO     支付信息
     * @return              支付消息信息
     */
    public static PayMessageDTO getPayMessageDTO(PayBaseBO payBaseBO, ResponseBO responseBO) {

        if (payBaseBO == null) {
            return null;
        }
        PayMessageDTO payMessageDTO = new PayMessageDTO();
        payMessageDTO.setPayNo(payBaseBO.getPayNo());
        payMessageDTO.setStatus(payBaseBO.getStatus());
        payMessageDTO.setRequestSystem(payBaseBO.getRequestSystem());
        payMessageDTO.setRequestBizNo(payBaseBO.getRequestBizNo());
        payMessageDTO.setRequestOrderNo(payBaseBO.getRequestOrderNo());
        payMessageDTO.setAmt(payBaseBO.getAmt());
        payMessageDTO.setErrorCode(responseBO.getErrorCode());
        payMessageDTO.setErrorMsg(responseBO.getErrorMsg());
        payMessageDTO.setRespDate(new Date());
        payMessageDTO.setFreezeCode(responseBO.getFreezeCode());
        payMessageDTO.setDescription(payBaseBO.getDescription());
        payMessageDTO.setTradeLogId(payBaseBO.getTradeLogId());
        payMessageDTO.setPaymentInfo(payBaseBO.getPaymentInfo());

        return payMessageDTO;
    }

    /**
     * 获取支付消息信息
     *
     * @param payBaseBO     支付信息
     * @return              支付消息信息
     */
    public static PayMessageDTO getPayMessageDTO(PayBaseBO payBaseBO) {

        if (payBaseBO == null) {
            return null;
        }
        PayMessageDTO payMessageDTO = new PayMessageDTO();
        payMessageDTO.setPayNo(payBaseBO.getPayNo());
        payMessageDTO.setStatus(payBaseBO.getStatus());
        payMessageDTO.setRequestSystem(payBaseBO.getRequestSystem());
        payMessageDTO.setRequestBizNo(payBaseBO.getRequestBizNo());
        payMessageDTO.setRequestOrderNo(payBaseBO.getRequestOrderNo());
        payMessageDTO.setAmt(payBaseBO.getAmt());
        payMessageDTO.setErrorCode(payBaseBO.getErrorCode());
        payMessageDTO.setErrorMsg(payBaseBO.getErrorMsg());
        payMessageDTO.setRespDate(new Date());
        payMessageDTO.setDescription(payBaseBO.getDescription());
        payMessageDTO.setTradeLogId(payBaseBO.getTradeLogId());
        payMessageDTO.setPaymentInfo(payBaseBO.getPaymentInfo());

        return payMessageDTO;
    }

    /**
     * 获取支付消息信息
     *
     * @param rechargeBO         充值支付信息
     * @param responseBO         返回信息
     */
    public static void getPayResponse(RechargeBO rechargeBO, ResponseBO responseBO) {

        responseBO.setRealFlag(true);
        responseBO.setStatus(rechargeBO.getStatus());

        responseBO.setErrorCode(rechargeBO.getErrorCode());
        responseBO.setErrorMsg(rechargeBO.getErrorMsg());
    }

    /**
     * 根据冻结资金单获取异常解冻资金单
     * @param freezeBO  冻结资金单
     * @return          异常解冻资金单
     */
    public static WithdrawFundBO freezeBO2ExUnfreezeBO(WithdrawFundBO freezeBO) {
        WithdrawFundBO exUnfreezeBO = new WithdrawFundBO();
        exUnfreezeBO.setPayNo(freezeBO.getPayNo());
        exUnfreezeBO.setStatus(PayStatusEnums.INIT.getCode());
        exUnfreezeBO.setAccountNo(freezeBO.getAccountNo());
        exUnfreezeBO.setPayToolType(PayToolTypeEnum.ACCOUNT.getCode());
        exUnfreezeBO.setAmt(freezeBO.getAmt());
        exUnfreezeBO.setCcy(freezeBO.getCcy());
        exUnfreezeBO.setAgreeRelId(freezeBO.getAgreeRelId());
        exUnfreezeBO.setAgreementNo(freezeBO.getAgreementNo());
        exUnfreezeBO.setBizFeeAccount(freezeBO.getAccountNo());
        exUnfreezeBO.setFundType(FundTypeEnum.EX_UNFREEZE_WITHDRAW.getCode());
        exUnfreezeBO.setReqAccAmt(freezeBO.getAmt());
        exUnfreezeBO.setSort(4);
        exUnfreezeBO.setFreezeCode(freezeBO.getFreezeCode());
        return exUnfreezeBO;
    }

    /**
     * 提现BO对象转换BackPayBO对象
     *
     * @param freezeBO     提现BO对象
     * @return               backPayBo
     */
    public static BackPayBO freezeBO2BackPayBO(FreezeBO freezeBO){

        BackPayBO backPayBO = new BackPayBO();
        if(null == freezeBO) {
            return backPayBO;
        }

        backPayBO.setPayNo(freezeBO.getPayNo());
        backPayBO.setRequestBizNo(freezeBO.getRequestBizNo());
        backPayBO.setRequestOrderNo(freezeBO.getRequestOrderNo());
        backPayBO.setRequestDate(freezeBO.getRequestDate());
        backPayBO.setRealFlag(freezeBO.isRealFlag());
        backPayBO.setCcy(freezeBO.getCcy());
        backPayBO.setAmt(freezeBO.getAmt());
        backPayBO.setResponseDate(new Date());
        backPayBO.setStatus(freezeBO.getStatus());
        backPayBO.setErrorMsg(freezeBO.getErrorMsg());
        backPayBO.setErrorCode(freezeBO.getErrorCode());
        backPayBO.setFreezeCode(freezeBO.getFreezeCode());

        return backPayBO;
    }
}
